package mysql

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func Mysql() {
	var err error
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := viper.GetString("Mysql.dsn")
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("Mysql 初始化成功")
	err = DB.AutoMigrate(&User{}, &RealName{})
	if err != nil {
		fmt.Println("数据迁移失败")
	} else {
		fmt.Println("数据迁移成功")
	}
}
